from .models import Areas
from rest_framework.response import Response
# Create your views here.

##########################################省市县三级联动 一级视图###################################################
# from rest_framework.views import APIView
# from .serializer import AreaInfoSerializer
#
# class AreasProviences(APIView):
#
#     def get(self,request):
#         area_set = Areas.objects.filter(parent=None).all()
#         serializer = AreaInfoSerializer(area_set,many=True)
#         return Response(serializer.data)
#
# class AreasSubCities(APIView):
#
#     def get(self,request,id):
#         area_set = Areas.objects.filter(parent=id).all()
#         serializer = AreaInfoSerializer(area_set,many=True)
#         return Response(serializer.data)
#
#

##########################################省市县三级联动 视图集###################################################
from rest_framework.viewsets import ReadOnlyModelViewSet
from .serializer import AreaInfoSerializer,AreaSubInfoSerializer
from rest_framework_extensions.mixins import CacheResponseMixin

class AreasViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    pagination_class = None  # 区划信息不分页

    def get_queryset(self):

       if self.action =='list':
           return  Areas.objects.filter(parent = None).all()
       else:
           return  Areas.objects.all()

    # @cache_response(timeout=3600,cache='default')
    def get_serializer_class(self):

        if self.action == 'list':
            return AreaInfoSerializer
        else:
            return AreaSubInfoSerializer

